package com.ruoyi.web.controller.erp.customs;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.erp.customs.domain.Customs;
import com.ruoyi.erp.transport.domian.TransportUnitType;
import com.ruoyi.erp.customs.service.CustomsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * yun.wang
 * 海关统计
 */
@Api("海关统计")
@Controller
@RequestMapping("/customs")
public class CustomsController extends BaseController {
    @Autowired
    CustomsService customsService;
    private String prefix = "erp/customs";

    @RequiresPermissions("customs:list:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/customs";
    }

    @ApiOperation("海关统计列表")
    @RequiresPermissions("customs:list:list")
    @RequestMapping(value = "list",method = {RequestMethod.POST})
    @ResponseBody
    public TableDataInfo selectIsoUnitAll(
            @RequestParam(value = "customsStatNo",required = false) String customsStatNo,
            @RequestParam(value = "customsUnitMeas",required = false) String customsUnitMeas,
            @RequestParam(value = "description",required = false) String description
    ){
        startPage();
        List<Customs> json =customsService.selectCustomsAll(
                customsStatNo,
                customsUnitMeas,
                description);
        return getDataTable(json);
    }


    /**
     * 新增海关统计
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存海关统计
     */
    @RequiresPermissions("customs:list:add")
    @Log(title = "海关统计", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Customs customs)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(customsService.checkCustomsCodeUnique(customs)))
        {
            return error("新增海关统计'" + customs.getCustomsStatNo() + "'失败，海关统计已存在");
        }
        customs.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(customsService.insertCustomst(customs));
    }


    /**
     * 修改海关统计
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String customsStatNo, ModelMap mmap)
    {
        mmap.put("customs", customsService.selectCustomsByCode(customsStatNo));
        return prefix + "/edit";
    }


    /**
     * 修改保存海关统计
     */
    @RequiresPermissions("customs:list:edit")
    @Log(title = "海关统计", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Customs customs)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(customsService.checkCustomsCodeUnique(customs)))
        {
            return error("修改海关统计'" + customs.getCustomsStatNo() + "'失败，运输海关统计已存在");
        }
        customs.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(customsService.updateCustomst(customs));
    }
    /**
     * 下载模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<Customs> util = new ExcelUtil<Customs>(Customs.class);
        return util.importTemplateExcel("海关统计数据");
    }

    @Log(title = "海关统计", businessType = BusinessType.IMPORT)
    @RequiresPermissions("customs:list:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Customs> util = new ExcelUtil<Customs>(Customs.class);
        List<Customs> transportList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = customsService.importTransport(transportList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @Log(title = "海关统计", businessType = BusinessType.EXPORT)
    @RequiresPermissions("customs:list:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(String ids)
    {
        List<Customs> list = customsService.selectCustomstList(ids);
        ExcelUtil<Customs> util = new ExcelUtil<Customs>(Customs.class);
        return util.exportExcel(list, "海关统计");
    }

    @RequiresPermissions("customs:list:remove")
    @Log(title = "海关统计", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(customsService.deleteCustomsByIds(ids));
    }

}
